<?php
/**
 * Extends the functionality of HMVC library's Module.php class
 *
 * @author Ishtiaque Shahrier and Hasnain Khan <md.istiaque@newgenbd.net, hasnain.khan@newgenbd.net>
 *
 * Services
 */

class user_service_l_user {


    /**
     * Creates a new user on registration.
     *
     * @param array , includes username, password, email
     * @return  constant
     */
    public static function create_user($array_data) {
        $u = new user_m_user();
        $u->username = $array_data['username'];
        $u->last_name = $array_data['last_name'];
        $u->first_name = $array_data['first_name'];
        $u->password = md5($array_data['password']);
        $u->email = $array_data['email'];
        $u->customer = isset($array_data['customer']) && strlen($array_data['customer']) ? $array_data['customer'] : NULL;
        $u->status_number = $array_data['status_number'];
        $u->save();
        return $u;
    }

    /**
     * Creates a new billing address on registration.
     *
     * @param   array        includes username, password, email
     * @param   int          user_id
     * @return  boolean      true if save was successful
     */
    public function add_user_billing_address($array_data, $user_id=0) {

        $a = new addresses_m_user();
        $a->user_id = $user_id;
        $a->address_1 = $array_data['billing_address_1'];
        $a->address_2 = $array_data['billing_address_2'] ; 
        $a->country = $array_data['billing_country'];
        $a->province = $array_data['billing_province'];
        $a->postal_code = $array_data['billing_postal_code'];
        $a->phone = $array_data['billing_phone'];
        $a->fax = $array_data['billing_fax'];
        $a->type = "B";
        
        return $a->save();
    }

    /**
     * Creates a new shipping address 
     *
     * @param   array        includes username, password, email
     * @param   int          user_id
     * @return  boolean      true if save was successful
     */
    public function add_user_shipping_address($array_data, $user_id=0) {

        $a = new addresses_m_user();
        $a->user_id = $user_id;
        $a->address_1 = $array_data['shipping_address_1'];
        $a->address_2 = $array_data['shipping_address_2'] ; //md5($array_data['shipping_address_2']);WHY MD5
        $a->country = $array_data['shipping_country'];
        $a->province = $array_data['shipping_province'];
        $a->postal_code = $array_data['shipping_postal_code'];
        $a->phone = $array_data['shipping_phone'];
        $a->fax = $array_data['shipping_fax'];
        $a->type = "S";

        return $a->save();
    }

    /**
     * Deletes user from the database
     *
     * @param   int          user_id
     * @return  boolean      true if user was deleted successfully
     */
    public function delete_user($user_id) {
        $u = new user_m_user();
        $u = $u->get_by_id($user_id);
        return $u->delete();
    }

    /**
     * Checks is user under the username exists alread.
     * Used for registration
     *
     * @param   int          username
     * @return  boolean      true if user exits
     */
    public static function user_exists($username) {
        $u = new user_m_user();
        $u->get_by_username($username);
        return $u->exists();
    }

    /**
     * Deletes user address
     *
     * @param   int          address_id
     * @return  boolean      true if address was deleted successfully
     */
    public function delete_user_address($address_id) {
        $a = new addresses_m_user();
        $a = $a->get_by_id($address_id);
        return $a->delete();
    }

    public function logout(){
        $user = get_logged_user();
        $user->last_login = date( 'Y-m-d H:i:s');
        $user->save();
        $objMySession = get_instance()->session;
        $objMySession->unset_userdata('current_login_userid');
        //@TO_DO CALL HOOKS for logout event
    }
    public function login($username, $password,$sys_login=FALSE) {
        $objMySession = get_instance()->session;
        $objUser = new user_m_user();
        $objUser->where("username", $username);
        $objUser->where("password", md5($password));
        if($sys_login===TRUE){
            $objUser->where("is_system_user", "1");
        }
        $objUser->get();
        if ($objUser->exists()) {
            $objMySession->set_userdata('current_login_userid', $objUser->id);
            return $objUser;
        } else {
            $objMySession->unset_userdata('current_login_userid');
            return null;
        }
    }

    /**
     * Returns the billing address
     *
     * @param   int/object   if user_id is passed then changes that into object
     * @return  object       returns billing object
     */
    public function get_billing_address($user_id_object) {

        $obj = $this->get_object($user_id_object);
        if ($obj !== NULL) {
            $shipping =
                            $obj->addresses_m_user
                            ->where("user_id", $obj->id)
                            ->where("type", addresses_m_user::BILLING)
                            ->get();

            if ($shipping->result_count() > 0) {
                return $shipping;
            } else {
                return NULL;
            }
        }
    }

    /**
     * Returns the list of shipping addresses.
     *
     * @param   int/object      user_id/ user object
     * @param   boolean         sets it to true
     * @return  object          list of all the shipping addresses
     */
    public function get_shiping_address($user_id_object, $only_default=TRUE) {
        $obj = $this->get_object($user_id_object);
        
        if ($obj !== NULL) {
            $shipping =
                            $obj->addresses_m_user
                            ->where("user_id", $obj->id)
                            ->where("type", addresses_m_user::SHIPPING)
//                            ->where("is_default", $only_default)
                            ->get();

            if ($shipping->result_count() > 0) {
                return $shipping;
            } else {
                return NULL;
            }
        }
    }

    /**
     * Returns the default shipping address.
     * Renders the call by setting default false
     *
     * @param   int/object      user_id/ user object
     * @return  object          returns billing object
     */
    public function get_shipping_addresses($user_id_object)
    {
        return $this->get_shiping_address($user_id_object, FALSE);
    }
    
    /**
     * Returns the default shipping address
     *
     * @param   int/object      
     * @return  object          returns billing object
     */
    public function get_default_shipping_address($user_id_object)
    {
        return $this->get_shiping_address($user_id_object);
    }

    /**
     * Returns the object by passing the id and model name
     *
     * @param   int         id
     * @param   string      name of the model
     * @return  object      returns object
     */
    public function get_object($user_id_object) {
       return get_object($user_id_object, "user_m_user");
    }

    /**
     * Returns the address object
     *
     * @param   int/object  address id or address object
     * @return  object      address object
     */
     public function get_address_object($address_id_object) {

        if (is_object($address_id_object)) {
            return $address_id_object;
        } else {
            $id = $address_id_object;
            $obj = new address_m_user();
            $obj->get_by_id($id);
            if ($obj->exists()) {
                return $obj;
            } else {
                return NULL;
            }
        }
    }

    /**
     * Edits the address
     *
     * @param   array       address information
     * @return  boolean     true if saved successful
     */
    public function edit_address($array_data) {
        $a = new addresses_m_user();
        $a->get_by_id($array_data['address_id']);

        if(strcmp($array_data['type'], "S")==0)
            $type="shipping_";
        else
            $type="billing_";

        $a->address_1 = $array_data[$type.'address_1'];
        $a->address_2 = $array_data[$type.'address_2'] ; 
        $a->country = $array_data[$type.'country'];
        $a->province = $array_data[$type.'province'];
        $a->postal_code = $array_data[$type.'postal_code'];
        $a->phone = $array_data[$type.'phone'];
        $a->fax = $array_data[$type.'fax'];
        
        return $a->save();
    }

    public function get_system_users(){
        $user = new user_m_user();
        $user->where("is_system_user","1")->get();
        if($user->result_count()>0){
            return $user;
        }else{
            NULL;
        }
    }

}

?>
